package com.dream.mqpushclient.controller;

import com.dream.mqpushclient.netty.NettyClient;
import com.dream.mqpushserver.netty.Request;
import com.dream.mqpushserver.netty.RequestCode;
import com.dream.mqpushserver.netty.RequestCommand;
import com.dream.mqpushserver.zookeeper.ZKserver;
import org.apache.zookeeper.KeeperException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import sun.security.krb5.internal.PAData;

/**
 * @author song_
 * @Date 2020/3/26
 * @desc
 **/
@RestController
public class TestController {

    @Autowired
    private ZKserver zKserver ;

    @Autowired
    private NettyClient nettyClient;

    @GetMapping(path = "/zk")
    public String testZkServer() throws KeeperException, InterruptedException {
        zKserver.createNode("/song/20200606","this is my test ", 1);
        return "success" ;
    }

    @GetMapping(path = "/test/{param}")
    public String test(@PathVariable("param") int num) {

        String str = "";
        for (int i = 0; i < num; i++) {
            str += "this is a send data " + i + "\t";
        }
        RequestCommand command = RequestCommand.buildRequestCommand(RequestCode.REGISTER, str.getBytes());
        return nettyClient.sendMsg(command);
    }

    @GetMapping(path = "/test2")
    public String test2(int num) {
        String str = "";
        for (int i = 0; i < num; i++) {
            str += "this is a client request data " + i + "\n";
        }
        Request request = new Request();
        request.setType((byte) 1);
        request.setLength(str.length());
        request.setContent(str);
        nettyClient.sendRequestMsg(request);
        return "success";

    }


    @GetMapping(path = "/sayHello")
    public String sayHello(String name ){

        nettyClient.sendHelloService(name);
        return "success" ;
    }
}
